//
//  HomeViewController.swift
//  SwiftDemo
//
//  Created by 宏泰财富 on 2018/11/8.
//  Copyright © 2018 宏泰财富. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {

    lazy var timeLab: UILabel = self.creatTimerLab()
    var timer: CADisplayLink?
    var conuntTime: Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.navigationBarBackgroungImage = UIImage.init(named: "Navbar_red_gradient")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "next", style: .plain, target: self, action: #selector(nextBtnClick))
        self.navigationItem.title = "首页"
        self.view.addSubview(self.timeLab)
        self.conuntTime = 0
        self.timer = CADisplayLink.init(target: self, selector: #selector(timerRuning))
        self.timer?.add(to: .main, forMode: .common)
        
       
       
    }
    @objc func nextBtnClick() {
//        let testVc: TestOneController = TestOneController()
//        self.navigationController?.pushViewController(testVc, animated: true)
        UIApplication.shared.open(URL.init(string: "weixin://")!, options: [:], completionHandler: nil)
    }
    
    @objc func timerRuning() {
        let showTime = self.formatTimeInterval(timeInterval: self.conuntTime)
        self.timeLab.text = showTime
        self.conuntTime += 1
    }
    
    private func creatTimerLab() -> UILabel{
        let lab: UILabel = UILabel.init(frame: CGRect.init(x: 100, y: 200, width: 150, height: 50))
        lab.text = "00:00"
        return lab
    }
    
    func formatTimeInterval(timeInterval: Int) -> String {
        var secondString: String
        var minString: String
        var timeString: String
      
        let second = timeInterval%60
        if  second>9 {
            secondString = String.init(format: "%d", second)
        } else{
            secondString = String.init(format: "0%d", second)
        }
        
        minString = String.init(format: "%d", timeInterval/60)
        timeString = String.init(format: "%d", timeInterval/3600)
        
        return String.init(format: "%@:%@:%@", secondString,minString, timeString)
    }

}
